package com.cennavi.config;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

import org.apache.spark.SparkConf;

import com.cennavi.util.sparkUtils;

/** * @author  作者 :allen
    * @date 创建时间：2017年9月20日 上午9:45:41 
    * @version 1.0 
    */

public class SparkConfigFile {

	private SparkConf sparkConf;
	private int windowDuration;
	private int slideDuration;
	private int windowDuration1;
	private int slideDuration1;
	private int runPartation;
	private long SparkStreamDurationsSeconds;
	
	public void init(String sparkconfigfilePath){
		Properties props = new Properties();
		try {
			props.load(new FileInputStream(sparkconfigfilePath));
			this.windowDuration = Integer.parseInt(props.getProperty("groupByKeyAndWindow.windowDuration"));
			this.slideDuration = Integer.parseInt(props.getProperty("groupByKeyAndWindow.slideDuration"));
			this.windowDuration1 = Integer.parseInt(props.getProperty("reduceByWindow.windowDuration"));
			this.slideDuration1 = Integer.parseInt(props.getProperty("reduceByWindow.slideDuration"));
			runPartation = Integer.parseInt(props.getProperty("spark.runPartation"));
			sparkConf = new SparkConf();
			ArrayList<String> list = sparkUtils.quietly(props.getProperty("jarFileFolder"), new ArrayList<String>());
			sparkConf.setJars(list.toArray(new String[list.size()]));
			//System.out.println(list.toString());
			sparkConf.setMaster(props.getProperty("spark.master"));
			sparkConf.setAppName(props.getProperty("jobName"));
			//配置队列
			sparkConf.set("spark.yarn.queue", props.getProperty("spark.yarn.queue"));
			SparkStreamDurationsSeconds = Long.parseLong(props.getProperty("SparkStream.Durations.Seconds"));
			//关闭推测执行
			sparkConf.set("spark.speculation", props.getProperty("spark.speculation"));
			//设置driver
			sparkConf.set("spark.driver.memory", props.getProperty("spark.driver.memory"));
			sparkConf.set("spark.driver.maxResultSize", props.getProperty("spark.driver.maxResultSize"));
			//设置executor
			sparkConf.set("spark.executor.instances", props.getProperty("spark.executor.instances"));
			sparkConf.set("spark.executor.cores", props.getProperty("spark.executor.cores"));
			sparkConf.set("spark.executor.memory", props.getProperty("spark.executor.memory"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public SparkConf getSparkConf() {
		return sparkConf;
	}
	public void setSparkConf(SparkConf sparkConf) {
		this.sparkConf = sparkConf;
	}
	public int getWindowDuration() {
		return windowDuration;
	}
	public void setWindowDuration(int windowDuration) {
		this.windowDuration = windowDuration;
	}
	public int getSlideDuration() {
		return slideDuration;
	}
	public void setSlideDuration(int slideDuration) {
		this.slideDuration = slideDuration;
	}

	public int getRunPartation() {
		return runPartation;
	}

	public void setRunPartation(int runPartation) {
		this.runPartation = runPartation;
	}

	public long getSparkStreamDurationsSeconds() {
		return SparkStreamDurationsSeconds;
	}

	public void setSparkStreamDurationsSeconds(long sparkStreamDurationsSeconds) {
		SparkStreamDurationsSeconds = sparkStreamDurationsSeconds;
	}

	public int getWindowDuration1() {
		return windowDuration1;
	}

	public void setWindowDuration1(int windowDuration1) {
		this.windowDuration1 = windowDuration1;
	}

	public int getSlideDuration1() {
		return slideDuration1;
	}

	public void setSlideDuration1(int slideDuration1) {
		this.slideDuration1 = slideDuration1;
	}
	
	
}
